package com.zy.pro.order.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zy.core.service.impl.ServiceSupport;
import com.zy.pro.letter.dao.LetterMapper;
import com.zy.pro.letter.model.Letter;
import com.zy.pro.order.OrderCnt;
import com.zy.pro.order.dao.OrderItemMapper;
import com.zy.pro.order.dao.OrderMoneyMapper;
import com.zy.pro.order.model.OrderItem;
import com.zy.pro.order.model.OrderMoney;
import com.zy.pro.order.service.OrderItemService;
import com.zy.pro.order.web.form.OrderItemSearchForm;
import com.zy.pro.shop.dao.ShopMapper;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.user.dao.UserMapper;
import com.zy.pro.user.model.User;
import com.zy.utils.BigDecimalUtil;
import com.zy.utils.UUIDHelper;
@Service
public class OrderItemServiceImpl extends ServiceSupport<OrderItem> implements OrderItemService {

	@Autowired
    private OrderItemMapper<OrderItem> mapper;
	
	@Autowired
    private ShopMapper<Shop> shopMapper;
	
	@Autowired
    private UserMapper<User> userMapper;
	
	@Autowired
    private OrderMoneyMapper<OrderMoney> orderMoneyMapper;
	
	@Autowired
    private LetterMapper<Letter> letterMapper;

	public OrderItemMapper<OrderItem> getMapper() {
		return mapper;
	}

	@Override
	public void updateOrderAddLetter(OrderItemSearchForm form) throws Exception {
		//卖家删除新预约订单发送站内信通知用户
		Letter letter = new Letter();
		//设置站内信标题
		letter.setLetterTitle("Delete order notice");
		//设置站内信创建时间
		letter.setLetterCreateTime(new Date());
		//设置站内信类型(1:用户发送,店家接收,2:店家发送,用户接收)
		letter.setLetterType(2);
		//设置站内信类型状态(1:普通,2:确认金额执行,3:付款,4:退款,5:删除)
		letter.setLetterTypeStatus(5);
		//设置站内信状态(1:正常,2:冻结,3:删除)
		letter.setLetterStatus(1);
		//设置站内信店铺阅读状态(0:发送1:未读,2:已读)
		letter.setLetterShopStatus(0);
		//设置站内信用户阅读状态(0:发送1:未读,2:已读)
		letter.setLetterUserStatus(1);
		//设置站内信主键
		letter.setLetterId(UUIDHelper.getUUID36());
		//设置站内信关联订单主键
		letter.setLetterOrderId(form.getOrderId());
		//订单的详细信息
		OrderItem orderItem = mapper.selectById(form.getOrderId());
		//发信店铺信息
		Shop shop = shopMapper.selectById(orderItem.getOrderShopId());
		//发信人主键
		letter.setLetterShopId(shop.getShopId());
		//发信人名称
		letter.setLetterShopName(shop.getShopName());
		//发信人联系方式
		letter.setLetterPhone(shop.getContactTel());
		//收信用户信息
		User user = userMapper.selectById(orderItem.getOrderUserId());
		//收信人主键
		letter.setLetterUserId(user.getUserId());
		//收信人名称
		letter.setLetterUserName(user.getRealName());
		//站内信信息
		letter.setLetterContent("Order No. "+form.getOrderId()+", has been seller to delete, if in doubt please contact the seller!");
		//新增站内信信息
		letterMapper.insert(letter);
		//更新订单状态为3:删除状态
		OrderItem order = new OrderItem();
		order.setOrderId(form.getOrderId());
		order.setOrderStatus(3);//1:未执行,2:执行中,3:删除状态,4:已完成
		mapper.updateBySelective(order);
	}

	@Override
	public void userPayment(OrderItemSearchForm form) throws Exception {
		//当前时间
		Date now = new Date();
		OrderItem order = mapper.selectById(form.getOrderId());
		//form.getOrderHasPayMoney()是当前本次用户所支付的金额
		//设置最新已支付金额(已付金额与本次金额相加)
		order.setOrderHasPayMoney(BigDecimalUtil.add(order.getOrderHasPayMoney(), form.getOrderThisMoney()));
		//设置最新未支付金额(未付金额减去本次金额)
		order.setOrderFailsPayMoney(BigDecimalUtil.sub(order.getOrderFailsPayMoney(), form.getOrderThisMoney()));
		//最新一次的付款时间
		order.setOrderMoneyTime(now);
		//设置本次应付金额为0
		order.setOrderThisMoney(new BigDecimal(0));
		//a.compareTo(b) 返回结果(-1:小于,0:等于,1:大于)
		if(order.getOrderFailsPayMoney().compareTo(new BigDecimal(0)) == 0){
			//比较未支付金额是否为0,为0,则表示付款完毕,修改订单支付状态为:4:完全支付
			if(order.getOrderMoneyStatus() == OrderCnt.ORDER_MONEY_PART_STATUS){
				order.setOrderMoneyStatus(OrderCnt.ORDER_MONEY_ALL_STATUS);
			}
		}else if(order.getOrderFailsPayMoney().compareTo(new BigDecimal(0)) == 1){
			//比较未支付金额是否为0,大于0,则表示付款未完毕,修改订单支付状态为:3:部分支付
			if(order.getOrderMoneyStatus() == OrderCnt.ORDER_MONEY_TOBE_STATUS){
				order.setOrderMoneyStatus(OrderCnt.ORDER_MONEY_PART_STATUS);
			}
		}
		
		//查询订单最新的一条付款记录
		/*OrderMoney mon = orderMoneyMapper.selectNewMoneyByOrderId(order.getOrderId());
		if(mon != null){
			
		}*/
		
		//设置订单付款记录
		OrderMoney money = new OrderMoney();
		//记录主键
		money.setMoneyId(UUIDHelper.getUUID36());
		//订单主键
		money.setMoneyOrderId(form.getOrderId());
		//设置当前次付款金额
		money.setMoneyThisPay(form.getOrderThisMoney());
		//设置已付金额
		money.setMoneyHasPay(order.getOrderHasPayMoney());
		//设置未付金额
		money.setMoneyFailsPay(order.getOrderFailsPayMoney());
		//付款时间
		money.setMoneyOrderTime(now);
		//创建时间
		money.setMoneyCreateTime(now);
		//插入付款记录
		orderMoneyMapper.insert(money);
		
		//更新订单信息
		mapper.updateBySelective(order);
		
		
		//执行付款发送站内信通知卖家
		Letter letter = new Letter();
		//设置站内信标题
		letter.setLetterTitle("Payment notice");
		//设置站内信创建时间
		letter.setLetterCreateTime(new Date());
		//设置站内信类型(1:用户发送,店家接收,2:店家发送,用户接收)
		letter.setLetterType(1);
		//设置站内信类型状态(1:普通,2:确认金额执行,3:付款,4:退款,5:删除)
		letter.setLetterTypeStatus(3);
		//设置站内信状态(1:正常,2:冻结,3:删除)
		letter.setLetterStatus(1);
		//设置站内信店铺阅读状态(0:发送1:未读,2:已读)
		letter.setLetterShopStatus(1);
		//设置站内信用户阅读状态(0:发送1:未读,2:已读)
		letter.setLetterUserStatus(0);
		//设置站内信主键
		letter.setLetterId(UUIDHelper.getUUID36());
		//设置站内信关联订单主键
		letter.setLetterOrderId(form.getOrderId());
		//收信店铺信息
		Shop shop = shopMapper.selectById(order.getOrderShopId());
		//收信人主键
		letter.setLetterShopId(shop.getShopId());
		//收信人名称
		letter.setLetterShopName(shop.getShopName());
		//发信用户信息
		User user = userMapper.selectById(order.getOrderUserId());
		//发信人主键
		letter.setLetterUserId(user.getUserId());
		//发信人名称
		letter.setLetterUserName(user.getRealName());
		//发信人联系方式
		letter.setLetterPhone(user.getUserPhone());
		//站内信信息
		letter.setLetterContent("Order No. "+form.getOrderId()+", have to pay, payment of '"+form.getOrderThisMoney()+"' dollars!");
		//新增站内信信息
		letterMapper.insert(letter);
	}

	@Override
	public OrderItem findOrderDetail(String orderId) {
		return mapper.findOrderDetail(orderId);
	}

	@Override
	public void updateOrderAndLetterShop(OrderItemSearchForm form)throws Exception {
		//用户删除执行中(未付款的订单)发送站内信通知卖家
		Letter letter = new Letter();
		//设置站内信标题
		letter.setLetterTitle("Delete order notice");
		//设置站内信创建时间
		letter.setLetterCreateTime(new Date());
		//设置站内信类型(1:用户发送,店家接收,2:店家发送,用户接收)
		letter.setLetterType(1);
		//设置站内信类型状态(1:普通,2:确认金额执行,3:付款,4:退款,5:删除)
		letter.setLetterTypeStatus(5);
		//设置站内信状态(1:正常,2:冻结,3:删除)
		letter.setLetterStatus(1);
		//设置站内信店铺阅读状态(0:发送1:未读,2:已读)
		letter.setLetterShopStatus(1);
		//设置站内信用户阅读状态(0:发送1:未读,2:已读)
		letter.setLetterUserStatus(0);
		//设置站内信主键
		letter.setLetterId(UUIDHelper.getUUID36());
		//设置站内信关联订单主键
		letter.setLetterOrderId(form.getOrderId());
		//订单的详细信息
		OrderItem orderItem = mapper.selectById(form.getOrderId());
		//收信店铺信息
		Shop shop = shopMapper.selectById(orderItem.getOrderShopId());
		//收信人主键
		letter.setLetterShopId(shop.getShopId());
		//收信人名称
		letter.setLetterShopName(shop.getShopName());
		//发信用户信息
		User user = userMapper.selectById(orderItem.getOrderUserId());
		//发信人主键
		letter.setLetterUserId(user.getUserId());
		//发信人名称
		letter.setLetterUserName(user.getRealName());
		//发信人联系方式
		letter.setLetterPhone(user.getUserPhone());
		//站内信信息
		letter.setLetterContent("Order No. "+form.getOrderId()+", has been user to delete, if in doubt please contact the user!");
		//新增站内信信息
		letterMapper.insert(letter);
		//更新订单状态为3:删除状态
		OrderItem order = new OrderItem();
		order.setOrderId(form.getOrderId());
		order.setOrderStatus(3);//1:未执行,2:执行中,3:删除状态,4:已完成
		mapper.updateBySelective(order);
	}

	@Override
	public void userApplyRefund(OrderItemSearchForm form) throws Exception {
		//用户申请执行中订单退款(已付款,未确认完成的订单,进行退款)发送站内信通知卖家
		Letter letter = new Letter();
		//设置站内信标题
		letter.setLetterTitle("Order refund notice");
		//设置站内信创建时间
		letter.setLetterCreateTime(new Date());
		//设置站内信类型(1:用户发送,店家接收,2:店家发送,用户接收)
		letter.setLetterType(1);
		//设置站内信类型状态(1:普通,2:确认金额执行,3:付款,4:退款,5:删除)
		letter.setLetterTypeStatus(4);
		//设置站内信状态(1:正常,2:冻结,3:删除)
		letter.setLetterStatus(1);
		//设置站内信店铺阅读状态(0:发送1:未读,2:已读)
		letter.setLetterShopStatus(1);
		//设置站内信用户阅读状态(0:发送1:未读,2:已读)
		letter.setLetterUserStatus(0);
		//设置站内信主键
		letter.setLetterId(UUIDHelper.getUUID36());
		//设置站内信关联订单主键
		letter.setLetterOrderId(form.getOrderId());
		//订单的详细信息
		OrderItem orderItem = mapper.selectById(form.getOrderId());
		//收信店铺信息
		Shop shop = shopMapper.selectById(orderItem.getOrderShopId());
		//收信人主键
		letter.setLetterShopId(shop.getShopId());
		//收信人名称
		letter.setLetterShopName(shop.getShopName());
		//发信用户信息
		User user = userMapper.selectById(orderItem.getOrderUserId());
		//发信人主键
		letter.setLetterUserId(user.getUserId());
		//发信人名称
		letter.setLetterUserName(user.getRealName());
		//发信人联系方式
		letter.setLetterPhone(user.getUserPhone());
		//站内信信息
		letter.setLetterContent("Order No. "+form.getOrderId()+" user apply for a refund!");
		//新增站内信信息
		letterMapper.insert(letter);
		//更新订单用户状态为:2:申请退款状态
		OrderItem order = new OrderItem();
		order.setOrderId(form.getOrderId());
		order.setOrderUserStatus(2);//1:正常,2:申请退款,3:删除
		mapper.updateBySelective(order);
		
		//调用paypal支付之前模拟流程,申请退款之后,直接退款成功
		//userRefund(form);
		/*order.setOrderMoneyStatus(OrderCnt.ORDER_MONEY_REFUND_STATUS);
		order.setOrderStatus(4);//1:未执行,2:执行中,3:已删除,4:已完成
*/	}
		
	/**
	 * 确认退款成功之后,修改订单退款成功后的几个状态
	 * 用户订单状态(由申请退款,改为正常)
	 * 订单支付状态(更改为已退款)
	 * 订单状态(更改为已完成)
	 * @param form
	 * @throws Exception
	 */
	public void userRefund(OrderItemSearchForm form) throws Exception{
		//更新订单用户状态为:2:申请退款状态
		OrderItem order = new OrderItem();
		order.setOrderId(form.getOrderId());
		//确认退款成功后,用户订单状态,改为'正常'
		order.setOrderUserStatus(1);//1:正常,2:申请退款,3:删除
		//调用paypal支付之前模拟流程,申请退款之后,直接退款成功
		order.setOrderMoneyStatus(OrderCnt.ORDER_MONEY_REFUND_STATUS);
		order.setOrderStatus(4);//1:未执行,2:执行中,3:已删除,4:已完成
		mapper.updateBySelective(order);
	}
}
